package com.ankiang.treeprint.fragment.components;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.ankiang.treeprint.R;
import com.ankiang.treeprint.base.BaseFragment;
import com.ankiang.treeprint.lib.Group;
import com.ankiang.treeprint.lib.TRDataManager;
import com.ankiang.treeprint.lib.annotation.Widget;
import com.ankiang.treeprint.model.TRGroupItem;
import com.ankiang.treeprint.model.TRItemNode;
import com.ankiang.treeprint.model.TRItemTree;
import com.ankiang.treeprint.view.TRListener;
import com.ankiang.treeprint.view.TRConfig;
import com.ankiang.treeprint.view.TRLayout;
import com.qmuiteam.qmui.alpha.QMUIAlphaImageButton;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIDrawableHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author cginechen
 * @date 2016-10-20
 */

@Widget(group = Group.Seekpic, widgetClass = TRTimeFlyFragment.class, iconRes = R.mipmap.icon_grid_anim_list_view)
public class TRHomeTreeFragment extends BaseFragment {


    private static final String TAG = "TRHomeTreeFragment";
    public static String KEY_TITLE = "KEY_TITLE";


    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;


    @BindView(R.id.tree_grid_layout)
    TRLayout mTRLayout;

    TRItemTree mItemTree;

    TRDataManager dataManager;

    @BindView(R.id.container)
    View container;

    @BindView(R.id.home_container)
    View homeContainer;

    private String title;

    // 配置
    private TRConfig conf;

    int group = -1; // 默认是大家族

    // 长按时使用的itemId
    private int itemId;

    public void setGroup(int group) {
        this.group = group;
    }


    // 右上角列表
    private QMUIListPopup mListPopup;
    private QMUIListPopup mItemNodeListPopup;
    private QMUIAlphaImageButton mRightButton;

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.tr_fragment_tree_layout, null);
        ButterKnife.bind(this, root);

        conf = TRConfig.getInstance();


        dataManager = TRDataManager.getInstance(getContext());

        // todo add demo data
        if (group == 0) { // group == 0 时，为默认数据，默认数据
            mItemTree = dataManager.getDefaultData();
            mItemTree.setGroupId(0);
        } else {
            mItemTree = dataManager.getData(String.valueOf(group));
            mItemTree.setGroupId(group);
        }


        mTRLayout.setItemTree(mItemTree);


        mTRLayout.setItemLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int id = (int) v.getTag();
                itemId = id;
                initItemNodeListPopupIfNeed();
                mItemNodeListPopup.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_CENTER);
                mItemNodeListPopup.setPreferredDirection(QMUIPopup.DIRECTION_TOP);
                mItemNodeListPopup.show(v);

                return true;
            }
        });
        mTRLayout.setItemTree(mItemTree);
        mTRLayout.setFullPercent(); // 默认打开时展示全部
        mTRLayout.initWithTree();
        dataManager.setData(String.valueOf(group), mTRLayout.getItemTree());
//        mTRLayout.flipScaleTreeGridLayout();


        initView();
        initTopBar();

        return root;

    }

    private void initView() {

    }


    /**
     * 更新标题
     * 打开时和编辑后更新
     */
    void initTitle() {
//        String title;
        TRGroupItem groupItem = dataManager.getGroup(group);
        title = groupItem.getName();
        if (title == null || title.trim().equals("")) {
            title = V(R.string.app_name);
        }
        if (group == 0) {
            mTopBar.setTitle(R.string.example_data);
        } else if (group == -1) {
            mTopBar.setTitle(R.string.home_big_family);
        } else {
            mTopBar.setTitle(title);
        }
    }

    void saveImage() {
        Bitmap createFromViewBitmap = QMUIDrawableHelper.createBitmapFromView(mTRLayout);
        QMUIDialog.CustomDialogBuilder dialogBuilder = new QMUIDialog.CustomDialogBuilder(getContext());
        dialogBuilder.setLayout(R.layout.drawablehelper_createfromview);
        final QMUIDialog dialog = dialogBuilder.setTitle(V(R.string.save_image)).create();
        ImageView displayImageView = (ImageView) dialog.findViewById(R.id.createFromViewDisplay);
        displayImageView.setImageBitmap(createFromViewBitmap);

        displayImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

        if (!isExternalStorageWritable()) {
            Toast.makeText(getContext(), "没有写权限！！！", Toast.LENGTH_SHORT).show();
            return;
        }
        File appDir = new File(Environment.getExternalStorageDirectory(), "Ankiang");
        if (!appDir.exists()) {
            if(!appDir.mkdir()){
                Toast.makeText(getContext(), "创建目录失败", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        String fileName = title + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            createFromViewBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            return;
        } catch (IOException e) {
            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            return;
        }catch (Exception e){
            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            return;
        }

        Toast.makeText(getContext(), "已经保存到: "+file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
    }


    //判断外部存储是否至少可以读
    //判断外部存储是否可以读写
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }


    void initTopBar() {

        initTitle();

        mRightButton = mTopBar.addRightImageButton(R.mipmap.icon_topbar_overflow, R.id.topbar_right_change_button);
        mRightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showRightButtonOps();
            }
        });

        mTopBar.addRightImageButton(R.mipmap.search, R.mipmap.search)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        showSearchPage();
                    }
                });

        mTopBar.addLeftBackImageButton().setOnClickListener(v -> {
            popBackStack();
        });

        mTopBar.addLeftImageButton(R.drawable.icon_bar_calculator, R.drawable.icon_bar_calculator)
                .setOnClickListener(v -> {
                    TRCalculatorFragment fragment = new TRCalculatorFragment();
                    startFragment(fragment);
                });
    }


    /**
     * 显示新增节点页面
     */
    private void showAddPage(int tag, int id) {


        TRItemNode data = new TRItemNode();
        data.setId(mItemTree.getNextId());

        TRItemNode otherData = new TRItemNode();
        otherData.setId(-1);

        TRItemNode item = mItemTree.get(id);
        int fid = 0;
        int mid = 0;
        if (item != null) {
            fid = item.getFid();
            mid = item.getMid();
        }
        switch (tag) {
            case TRAddItemFragment.TAG_FATHER:
                //data.setFid(id);
                if (mItemTree.hasFather(item)) {
                    Toast.makeText(getContext(), "已经有父节点，无法添加", Toast.LENGTH_SHORT).show();
                    return;
                }

                data.setSex(TRItemNode.Sex.MALE);
                otherData = item.clone();
                otherData.setFid(data.getId());

                break;
            case TRAddItemFragment.TAG_MOTHER:
//                data.setMid(id);
                if (mItemTree.hasMother(item)) {
                    Toast.makeText(getContext(), "已经有母节点，无法添加", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (mItemTree.hasFather(item) && mItemTree.getFather(item).getSex() == TRItemNode.Sex.COUPLE) {
                    Toast.makeText(getContext(), "结合节点的子辈，无法添加母辈", Toast.LENGTH_SHORT).show();
                    return;
                }
                data.setSex(TRItemNode.Sex.FEMALE);
                otherData = item.clone();
                otherData.setMid(data.getId());
                break;
            case TRAddItemFragment.TAG_BROTHER:
                data.setFid(fid);
                data.setMid(mid);
                data.setSex(TRItemNode.Sex.MALE);
                otherData = null;
                break;
            case TRAddItemFragment.TAG_ELER_BROTHER:
                data.setFid(fid);
                data.setMid(mid);
                data.setSex(TRItemNode.Sex.MALE);
                otherData = null;
                break;
            case TRAddItemFragment.TAG_SISTER:
                data.setFid(fid);
                data.setMid(mid);
                data.setSex(TRItemNode.Sex.FEMALE);
                otherData = null;
                break;
            case TRAddItemFragment.TAG_ELDER_SISTER:
                data.setFid(fid);
                data.setMid(mid);
                data.setSex(TRItemNode.Sex.FEMALE);
                otherData = null;
                break;
            case TRAddItemFragment.TAG_DAUGHTER:
                data.setFid(id);
                data.setSex(TRItemNode.Sex.FEMALE);
                otherData = null;
                break;
            case TRAddItemFragment.TAG_SON:
                data.setFid(id);
                data.setSex(TRItemNode.Sex.MALE);
                otherData = null;
                break;
            case TRAddItemFragment.TAG_HUSBAND:
                if (mItemTree.hasFatherAndMother(item)) {
                    Toast.makeText(getContext(), "结合节点不能添加夫妻", Toast.LENGTH_SHORT).show();
                    return;
                }
                data.setSex(TRItemNode.Sex.MALE);
                otherData.setFid(data.getId());
                otherData.setMid(item.getId());
                otherData.setSex(TRItemNode.Sex.COUPLE);
                otherData.setId(mItemTree.getNextId() + 1);
                break;
            case TRAddItemFragment.TAG_WIFE:
                if (mItemTree.hasFatherAndMother(item)) {
                    Toast.makeText(getContext(), "结合节点不能添加夫妻", Toast.LENGTH_SHORT).show();
                    return;
                }
                data.setSex(TRItemNode.Sex.FEMALE);
                otherData.setMid(data.getId());
                otherData.setFid(item.getId());
                otherData.setSex(TRItemNode.Sex.COUPLE);
                otherData.setId(mItemTree.getNextId() + 1);
                break;
            case TRAddItemFragment.TAG_MARRY:
                data.setSex(TRItemNode.Sex.COUPLE);
                if (item.getSex() == TRItemNode.Sex.FEMALE) {
                    data.setMid(item.getId());
                } else if (item.getSex() == TRItemNode.Sex.MALE) {
                    data.setFid(item.getId());
                }
                otherData = null;
                break;
        }

        TRAddItemFragment fragment = new TRAddItemFragment();


        fragment.setOp(TRAddItemFragment.ADD);
        Bundle bundle = new Bundle();
        bundle.putInt(TRAddItemFragment.KEY_TAG, tag);
        bundle.putSerializable(TRAddItemFragment.KEY_DATA, data);
        bundle.putSerializable(TRAddItemFragment.KEY_OTHER_DATA, otherData);
        fragment.setArguments(bundle);

        fragment.setAfterOperation((xid) -> {
            mTRLayout.setItemTree(mItemTree);
            mTRLayout.initWithTree();
            dataManager.setData(group, mItemTree);
            //dataManager.save();

        });
//        fragment.setData(data);
        fragment.setItemTree(mItemTree);
//        fragment.setItemId(0);
        startFragment(fragment);
    }

    /**
     * 显示搜索页面
     */
    private void showSearchPage() {
        TRListener listener = new TRListener() {
            @Override
            public void afterOperation(int id) {

//                Bundle args = new Bundle();
//                args.putInt("id", id);
                TRAddItemFragment fragment = new TRAddItemFragment();
                fragment.setOp(TRAddItemFragment.EDIT);
                TRItemNode data = mItemTree.get(id);
                Bundle bundle = new Bundle();
                bundle.putInt(TRAddItemFragment.KEY_TAG, TRAddItemFragment.TAG_DETAILS);
                bundle.putSerializable(TRAddItemFragment.KEY_DATA, data);
                bundle.putSerializable(TRAddItemFragment.KEY_OTHER_DATA, null);
                fragment.setArguments(bundle);
//                fragment.setArguments(args);

                startFragmentAndDestroyCurrent(fragment);
            }
        };


        // 传递数据
        TRSearchItemFragment fragmentSearch = new TRSearchItemFragment();

        // 设置为读状态，可以在搜索页面打开详情
        // 也避免了fragment not attached的问题
        fragmentSearch.setOp(TRSearchItemFragment.SEARCH_READ);
        fragmentSearch.setItemTree(mItemTree);
        fragmentSearch.setSearchCallBack(listener);
        startFragment(fragmentSearch);
    }


    protected String getTitle() {
        return V(R.string.app_name);
    }


    // --------------------------------------------------
    // 操作
    private void showRightButtonOps() {

        View v = mRightButton;
        initListPopupIfNeed();
        mListPopup.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_CENTER);
        mListPopup.setPreferredDirection(QMUIPopup.DIRECTION_TOP);
        mListPopup.show(v);


        mTopBar.setOnLongClickListener(x -> {
            TRShowTextFragment fragment = new TRShowTextFragment();
            fragment.setOp(TRShowTextFragment.OP_SHOW_ITEM_LIST);
            String content = mItemTree.toString();
            fragment.setContent(content);
            fragment.setId(group);
            startFragment(fragment);
            return true;
        });

    }


    private void initListPopupIfNeed() {
        if (mListPopup == null) {

            ArrayList<String> listItems = new ArrayList();
            listItems.add(V(R.string.details_op_add));
            listItems.add(V(R.string.edit_group));
            listItems.add(V(R.string.home_op_save));
            listItems.add(V(R.string.more));

//            // 没有数据，则展示 添加默认数据
//            if (mItemTree.isEmpty()) {
//                listItems.add(V(R.string.details_op_add_example_data));
//            }


            ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.simple_list_item, listItems);

            mListPopup = new QMUIListPopup(getContext(), QMUIPopup.DIRECTION_NONE, adapter);
            mListPopup.create(QMUIDisplayHelper.dp2px(getContext(), 120), QMUIDisplayHelper.dp2px(getContext(), 500), new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    switch (i) {
                        case 0:
                            // 新增加
                            showAddPage(TRAddItemFragment.TAG_NEW, 0);
                            break;
                        case 1:
                            // 新增加
                            showAddGroupPage(group);
                            break;
                        case 2:
                            // 刷新树，用于更新之后的操作
                            dataManager.save();
                            mTRLayout.setItemTree(mItemTree);
                            mTRLayout.initWithTree();
                            if (group == 0) {
                                Toast.makeText(getContext(), "保存失败: 示例数据不能保存", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getContext(), "保存成功", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 3:
                            // 展示更多
                            showMoreChoice();
                            break;
                    }
                    mListPopup.dismiss();
                }
            });
            mListPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                }
            });
        }
    }

    /**
     * 长按节点时的弹窗
     */
    private void initItemNodeListPopupIfNeed() {
        if (mItemNodeListPopup == null) {

            ArrayList<String> listItems = new ArrayList();
            listItems.add(V(R.string.details));
            listItems.add(V(R.string.details_op_add));
            listItems.add(V(R.string.details_op_delete));


            ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.simple_list_item, listItems);

            mItemNodeListPopup = new QMUIListPopup(getContext(), QMUIPopup.DIRECTION_NONE, adapter);
            mItemNodeListPopup.create(QMUIDisplayHelper.dp2px(getContext(), 120), QMUIDisplayHelper.dp2px(getContext(), 200), new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    //Toast.makeText(getContext(), "Item " + (i + 1), Toast.LENGTH_SHORT).show();


                    int id = itemId;
                    switch (i) {
                        case 0: // 查看详情
                            showDetailsPage(id);
                            break;
                        case 1:
                            // 先保存数据，然后再展示
                            dataManager.save();
                            showBottomSheetAdd(id);

                            // 新增加
                            //showAddChoice();
                            break;
                        case 2: // 删除
                            showMessageNegativeDeleteDialog(id);
                            break;
                        default:
                            Toast.makeText(getContext(), "没有这个操作！", Toast.LENGTH_SHORT).show();
                            break;
                    }
                    mItemNodeListPopup.dismiss();
                }
            });
            mItemNodeListPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                }
            });
        }
    }

    private void addExampleData() {
        if (!mItemTree.isEmpty()) {
            Toast.makeText(getContext(), "已有数据，无法添加示例数据", Toast.LENGTH_SHORT).show();
            return;
        }
        mItemTree = dataManager.getDefaultData();
        mTRLayout.setItemTree(mItemTree);
        mTRLayout.initWithTree();
        dataManager.setData(String.valueOf(group), mTRLayout.getItemTree());
    }

    // 显示详情页面
    private void showDetailsPage(int id) {

        if (!mItemTree.containsKey(id)) {
            return;
        }

        TRListener listener = xid -> {
            mTRLayout.setItemTree(mItemTree);
            mTRLayout.initWithTree();
        };
        TRAddItemFragment fragment = new TRAddItemFragment();
        fragment.setOp(TRAddItemFragment.EDIT);
        TRItemNode data = mItemTree.get(id);
        Bundle bundle = new Bundle();
        bundle.putInt(TRAddItemFragment.KEY_TAG, TRAddItemFragment.TAG_DETAILS);
        bundle.putSerializable(TRAddItemFragment.KEY_DATA, data);
        bundle.putSerializable(TRAddItemFragment.KEY_OTHER_DATA, null);
        fragment.setArguments(bundle);
        fragment.setAfterOperation(listener);
        fragment.setItemTree(mItemTree);
//        fragment.setItemId(id);
        startFragment(fragment);
    }


    // 删除确认弹窗
    private void showMessageNegativeDeleteDialog(final int itemId) {
        new QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle(V(R.string.details_op_delete))
                .setMessage(V(R.string.details_op_delete_confirm_tips))
                .addAction(V(R.string.details_op_cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, V(R.string.details_op_delete), QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        mItemTree.remove(itemId);

                        Toast.makeText(getActivity(), V(R.string.details_op_delete_success), Toast.LENGTH_SHORT).show();
                        dialog.dismiss();

                        // 更新数据
                        mTRLayout.setItemTree(mItemTree); // 重新刷新
                        mTRLayout.initWithTree();

                        // 保存数据
                        dataManager.setData(group, mItemTree);


                    }
                })
                .create().show();
    }


    // 删除分组确认弹窗
    private void showAddGroupPage(final int itemId) {
        TRAddGroupFragment fragment = new TRAddGroupFragment();
        fragment.setOp(TRAddGroupFragment.EDIT);

        Bundle bundle = new Bundle();
        TRGroupItem data = dataManager.getGroup(group);
        int tag = TRAddGroupFragment.EDIT;
        bundle.putInt(TRAddGroupFragment.KEY_TAG, tag);
        bundle.putSerializable(TRAddGroupFragment.KEY_DATA, data);
        fragment.setArguments(bundle);

        fragment.setAfterOperation(id -> {
            this.initTitle();
            TRHomeTreeFragment.this.runCallback(group);

        });
        startFragment(fragment);

    }


    private void showMoreChoice() {

        // todo 要用父母兄弟夫妻子女的办法
        new QMUIBottomSheet.BottomListSheetBuilder(getContext())
                .addItem(V(R.string.tree_op_amplify))
                .addItem(V(R.string.tree_op_narrow))
                .addItem(V(R.string.save_image))
                .addItem(V(R.string.details_op_add_example_data))
                .addItem(V(R.string.me_func_setting))
                .addItem(V(R.string.delete_group))
                .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                    @Override
                    public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                        switch (position) {
                            case 0:
                                // 放大
                                mTRLayout.reScale(1);
                                break;
                            case 1:
                                // 缩小
                                mTRLayout.reScale(-1);
                                break;
                            case 2:
                                // 导出图片
                                saveImage();
                                dialog.dismiss();
                                break;
                            case 3:// 添加示例数据
                                addExampleData();
                                dialog.dismiss();
                                break;
                            case 4:
                                showSettingsPage();
                                dialog.dismiss();
                                break;
                            case 5:
                                showMessageNegativeDeleteGroupDialog(group);
                                dialog.dismiss();
                                break;
                        }
                    }
                })
                .build().show();
    }


    // --------------------------------------------------
    // 长按节点操作时的操作
    private void showBottomSheetAdd(int id) {

        QMUIBottomSheet.BottomGridSheetBuilder builder = new QMUIBottomSheet.BottomGridSheetBuilder(getActivity());
        builder.addItem(R.mipmap.node_father, "父", TRAddItemFragment.TAG_FATHER, QMUIBottomSheet.BottomGridSheetBuilder.FIRST_LINE)
                .addItem(R.mipmap.node_mother, "母", TRAddItemFragment.TAG_MOTHER, QMUIBottomSheet.BottomGridSheetBuilder.SECOND_LINE)
                .addItem(R.mipmap.node_son, "子", TRAddItemFragment.TAG_SON, QMUIBottomSheet.BottomGridSheetBuilder.FIRST_LINE)
                .addItem(R.mipmap.node_daughter, "女", TRAddItemFragment.TAG_DAUGHTER, QMUIBottomSheet.BottomGridSheetBuilder.SECOND_LINE)
                .addItem(R.mipmap.node_husband, "夫", TRAddItemFragment.TAG_HUSBAND, QMUIBottomSheet.BottomGridSheetBuilder.FIRST_LINE)
                .addItem(R.mipmap.node_wife, "妻", TRAddItemFragment.TAG_WIFE, QMUIBottomSheet.BottomGridSheetBuilder.SECOND_LINE)
                .addItem(R.mipmap.node_marry, "结合", TRAddItemFragment.TAG_MARRY, QMUIBottomSheet.BottomGridSheetBuilder.FIRST_LINE)
                .addItem(R.mipmap.node_new, "新增", TRAddItemFragment.TAG_NEW, QMUIBottomSheet.BottomGridSheetBuilder.SECOND_LINE)
                .setOnSheetItemClickListener((dialog, itemView) -> {

                    int tag = (int) itemView.getTag();
                    showAddPage(tag, id);
                    dialog.dismiss();

                })
                .build().show();
    }


    /**
     * 显示设置页面
     */
    private void showSettingsPage() {
        TRSettingsFragment fragment = new TRSettingsFragment();
        fragment.setCallback(id -> {
            this.mTRLayout.initWithTree();
        });
        startFragment(fragment);
    }


    // 删除分组确认弹窗
    private void showMessageNegativeDeleteGroupDialog(final int itemId) {

        new QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle(V(R.string.details_op_delete))
                .setMessage(V(R.string.details_op_delete_confirm_tips))
                .addAction(V(R.string.details_op_cancel), new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, V(R.string.details_op_delete), QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {

                        Toast.makeText(getActivity(), V(R.string.details_op_delete_success), Toast.LENGTH_SHORT).show();
                        dialog.dismiss();

                        // 保存数据
                        dataManager.deleteData(group);
                        if (callback != null) {
                            callback.afterOperation(group);
                        }
                        popBackStack();
                    }
                })
                .create().show();
    }

}
